﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using LRSTaxServices.DAL;
using LRSTaxServices.DAL.dsContactUsTableAdapters;
using LRSTaxServices.Common;

namespace LRSTaxServices.Biz
{
    /// <summary>
    /// ContactUs Business Object
    /// </summary>

    public class ContactUs : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected ContactUs() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the ContactUs.
        /// </summary>
        public int ContactUsID
        {
            get { return _ContactUsID; }
            set
            {
                if (_ContactUsID != value)
                {
                    if (OnPropertyChanging("ContactUsID", _ContactUsID, value))
                    {
                        int oldValue = _ContactUsID;
                        _ContactUsID = value;

                        OnPropertyChanged("ContactUsID", oldValue, value);
                    }
                }
            }
        }  private int _ContactUsID = -1;




        /// <summary>
        /// FirstName.
        /// </summary>
        public string FirstName
        {
            get { return _firstname; }
            set
            {
                if (_firstname != value)
                {
                    if (OnPropertyChanging("FirstName", _firstname, value))
                    {
                        string oldValue = _firstname;
                        _firstname = value;
                        OnPropertyChanged("FirstName", oldValue, value);
                    }
                }
            }
        }  private string _firstname = null;



        /// <summary>
        /// LastName.
        /// </summary>
        public string LastName
        {
            get { return _lastname; }
            set
            {
                if (_lastname != value)
                {
                    if (OnPropertyChanging("LastName", _lastname, value))
                    {
                        string oldValue = _lastname;
                        _lastname = value;
                        OnPropertyChanged("LastName", oldValue, value);
                    }
                }
            }
        }  private string _lastname = null;



        /// <summary>
        /// Contact Phone
        /// </summary>
        public string ContactPhone
        {
            get { return _contactphone; }
            set
            {
                if (_contactphone != value)
                {
                    if (OnPropertyChanging("ContactPhone", _contactphone, value))
                    {
                        string oldValue = _contactphone;
                        _contactphone = value;
                        OnPropertyChanged("ContactPhone", oldValue, value);
                    }
                }
            }
        }  private string _contactphone = null;



        /// <summary>
        /// EmailAddress.
        /// </summary>
        public string EmailAddress
        {
            get { return _emailaddress; }
            set
            {
                if (_emailaddress != value)
                {
                    if (OnPropertyChanging("EmailAddress", _emailaddress, value))
                    {
                        string oldValue = _emailaddress;
                        _emailaddress = value;
                        OnPropertyChanged("EmailAddress", oldValue, value);
                    }
                }
            }
        }  private string _emailaddress = null;
        


        /// <summary>
        /// Address1.
        /// </summary>
        public string Address1
        {
            get { return __address1; }
            set
            {
                if (__address1 != value)
                {
                    if (OnPropertyChanging("Address1", __address1, value))
                    {
                        string oldValue = __address1;
                        __address1 = value;
                        OnPropertyChanged("Address1", oldValue, value);
                    }
                }
            }
        }  private string __address1 = null;



        /// <summary>
        /// Address2.
        /// </summary>
        public string Address2
        {
            get { return __address2; }
            set
            {
                if (__address2 != value)
                {
                    if (OnPropertyChanging("Address2", __address2, value))
                    {
                        string oldValue = __address2;
                        __address2 = value;
                        OnPropertyChanged("Address2", oldValue, value);
                    }
                }
            }
        }  private string __address2 = null;



        /// <summary>
        /// City.
        /// </summary>
        public string City
        {
            get { return __city; }
            set
            {
                if (__city != value)
                {
                    if (OnPropertyChanging("City", __city, value))
                    {
                        string oldValue = __city;
                        __city = value;
                        OnPropertyChanged("City", oldValue, value);
                    }
                }
            }
        }  private string __city = null;



        /// <summary>
        /// State.
        /// </summary>
        public string State
        {
            get { return _state; }
            set
            {
                if (_state != value)
                {
                    if (OnPropertyChanging("State", _state, value))
                    {
                        string oldValue = _state;
                        _state = value;
                        OnPropertyChanged("State", oldValue, value);
                    }
                }
            }
        }  private string _state = null;



        /// <summary>
        /// Messages.
        /// </summary>
        public string Messages
        {
            get { return _messages; }
            set
            {
                if (_messages != value)
                {
                    if (OnPropertyChanging("Messages", _messages, value))
                    {
                        string oldValue = _messages;
                        _messages = value;
                        OnPropertyChanged("Messages", oldValue, value);
                    }
                }
            }
        }  private string _messages = null;



        /// <summary>
        /// Date/time at which the ContactUs was created.
        /// </summary>
        public DateTime? AddedDate
        {
            get { return _addedDate; }
            set
            {
                if (_addedDate != value)
                {
                    if (OnPropertyChanging("AddedDate", _addedDate, value))
                    {
                        DateTime? oldValue = _addedDate;
                        _addedDate = value;
                        OnPropertyChanged("AddedDate", oldValue, value);
                    }
                }
            }
        }  private DateTime? _addedDate = null;

        




        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static ContactUs CreateNew()
        {
            ContactUs obj = new ContactUs();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.AddedDate = DateTime.Now;
            
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static ContactUs CreateNew(ContactUsCollection collection)
        {
            ContactUs obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (ContactUs ContactUs in collection)
            {
                if (ContactUs.ContactUsID < minVal)
                    minVal = ContactUs.ContactUsID;
            }
            obj.ContactUsID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllContactUs

        /// <summary>
        /// Returns a collection of all ContactUs.
        /// </summary>
        public static ContactUsCollection GetAllContactUs()
        {
            ContactUsTableAdapter adapter = new ContactUsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsContactUs.ContactUsDataTable tbl = adapter.GetData(null);

            ContactUsCollection list = new ContactUsCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsContactUs.ContactUsRow row in tbl.Rows)
                {
                    ContactUs ContactUs = new ContactUs();
                    RelationalToObject(ContactUs, row);
                    list.Add(ContactUs);
                }
            }

            return (list);
        }

        #endregion

        #region GetContactUsByID

        /// <summary>
        /// Returns an ContactUs object for the specified 'ContactUsID' or null if no ContactUs with the specified parameter(s) exists.
        /// </summary>
        public static ContactUs GetContactUsByID(int ContactUsid)
        {
            ContactUsTableAdapter adapter = new ContactUsTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsContactUs.ContactUsDataTable tbl = adapter.GetData(ContactUsid);

            if (tbl.Rows.Count == 1)
            {
                ContactUs ContactUs = new ContactUs();
                RelationalToObject(ContactUs, tbl.Rows[0]);
                return (ContactUs);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'ContactUs' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified ContactUs.
        /// </summary>
        public static void Update(ContactUs obj)
        {
            bool updateContactUs = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateContactUs = (obj.ObjState != ObjectState.Unchanged);

                if (!updateContactUs)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsContactUs.ContactUsDataTable tbl = null;
            if (updateContactUs)
            {
                ContactUsTableAdapter adapter = new ContactUsTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsContactUs.ContactUsDataTable();
                dsContactUs.ContactUsRow row = tbl.NewContactUsRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (ContactUsID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("ContactUsID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // FirstName is required.
                if ((FirstName == null) || (FirstName.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("FirstName", "FirstName is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region ToString

        /// <summary>
        /// User-friendly string represetation of this object.
        /// </summary>
        public override string ToString()
        {
            return (String.Format("ContactUs: ContactUsID={0}, EmailAddress={1}, {2}", ContactUsID, FirstName, EmailAddress));
        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more ContactUs objects.
    /// </summary>

    public class ContactUsCollection : List<ContactUs>
    {
        #region Types

        public class UserComparer : IComparer<ContactUs>
        {
            public int Compare(ContactUs q1, ContactUs q2)
            {
                return (String.Compare(q1.FirstName, q2.FirstName));
            }
        }

        #endregion

        #region Public Methods

        #region FindByContactUsID

        public ContactUs FindByContactUsID(int ContactUsID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].ContactUsID == ContactUsID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindContactUsByFirstName

        /// <summary>
        /// Case insensitve compare.
        /// </summary>
        public ContactUs FindContactUsByFirstName(string firstname)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].FirstName == firstname)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
